package entity

import (
	"fmt"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//ShLrjs
type ShLrjs struct {
	JLID     int     `gorm:"AUTO_INCREMENT;primary_key;column:JLID"`
	KHNF     int     `gorm:"column:KHNF"`
	UTID     int     `gorm:"column:UTID"`
	UTNM     string  `gorm:"column:UTNM"`
	ZLRJS    float64 `gorm:"column:ZLRJS"`
	EMNO     string  `gorm:"column:EMNO"`
	EMNM     string  `gorm:"column:EMNM"`
	EMID     int     `gorm:"column:EMID"`
	GSLRJS   float64 `gorm:"column:GSLRJS"`
	LSLRJS   float64 `gorm:"column:LSLRJS"`
	JRSCLRJS float64 `gorm:"column:JRSCLRJS"`
}

//TableName
func (ShLrjs) TableName() string {
	return "SH_JHCW_LRJS"
}

// BeforeCreate
func (u *ShLrjs) BeforeCreate(tx *gorm.DB) (err error) {
	var emid *int
	tx.Raw(`select "ID" from "T_SysUser" where "SysUserID" = ?`, u.EMNO).Row().Scan(&emid)
	if emid == nil {
		err = fmt.Errorf("员工号%s在系统中不存在！", u.EMNO)
		return
	}
	u.EMID = *emid
	return
}

// BeforeSave
func (s *ShLrjs) BeforeSave(tx *gorm.DB) (err error) {
	err = tx.Raw(`SELECT "ID" FROM "T_Organization" WHERE "OrganName" = ?`, s.UTNM).Row().Scan(&s.UTID)
	if err != nil {
		err = fmt.Errorf("机构名称%s在系统中不存在！", s.UTNM)
		return
	}
	return
}

func ShLrjsEF() base.EntityFunc {
	return func() interface{} {
		entity := ShLrjs{}
		return &entity
	}
}
func ShLrjsESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShLrjs
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("ShLrjs", base.NewDefaultDao(ShLrjsEF(), ShLrjsESF(), nil, "my"))
}
